package com.kdgz.kdgz.util;

import com.kdgz.kdgz.beans.Orders;
import com.kdgz.kdgz.beans.Staff;
import com.kdgz.kdgz.beans.StaffOrders;
import com.kdgz.kdgz.mapper.StaffMapper;
import com.kdgz.kdgz.mapper.StaffOrderMapper;
import com.kdgz.kdgz.sevice.OrdersService;
import com.kdgz.kdgz.sevice.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

/**
 * @Author Veneno
 * @Desc 发货
 * @date 2021/3/23
 */
@Component
public class SendOrders {
    @Autowired
    OrdersService ordersService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private StaffOrderMapper staffOrderMapper;

    public int send(Staff staff) {
        // 获得未分配的快递
        List<Orders> ordersForSend = ordersService.getOrdersForSend();
        int random=new Random().nextInt(5000)+5000;
        int num=0;
        for (Orders order : ordersForSend) {
            // 处于库中的快递才能分配
            if (order.getStatus() == 0&& order.getAid().equals(staff.getAid())){
                    // 放入staff_orders表中
                staffOrderMapper.insert(new StaffOrders(staff.getSid(), order.getOid()));
                num++;
                if(num==random){
                    break;
                }
            }
        }
        return random;
    }
}
